from PySide2.QtCore import Qt, Signal
from PySide2.QtGui import QPixmap, QPainter, QFont, QFontDatabase
from PySide2.QtWidgets import QWidget, QLabel

from . import resources_rc


class WidgetWelcome(QWidget):
    doubleClicked = Signal()

    def __init__(self):
        super().__init__()
        self.background = QPixmap(':/images/bg')
        self.banner = QLabel('地质导向角差轨迹计算软件', self)
        font_id = QFontDatabase.addApplicationFont(":/fonts/STLITI")
        if font_id == -1:
            self.font = QFont()
        else:
            font_family = QFontDatabase.applicationFontFamilies(font_id)[0]
            self.font = QFont(font_family)
        self.font.setBold(True)
        self.banner.setStyleSheet("color: white;background-color: rgba(0, 0, 0, 0.3);")
        self.setToolTip('双击进入程序')

    def paintEvent(self, event):
        """ paintEvent(self, event: PySide2.QtGui.QPaintEvent) -> None """
        super().paintEvent(event)  # 先调用父类方法
        painter = QPainter(self)
        pixmap = self.background.scaled(
            self.size(),
            Qt.KeepAspectRatioByExpanding,
            Qt.SmoothTransformation
        )

        x = (pixmap.width() - self.width()) / 2
        y = (pixmap.height() - self.height()) / 2
        painter.drawPixmap(self.rect(), pixmap, pixmap.rect().adjusted(x, y, -x, -y))

    def resizeEvent(self, event):
        """ resizeEvent(self, event: PySide2.QtGui.QResizeEvent) -> None """
        super().resizeEvent(event)  # 先调用父类方法

        # 获取窗口尺寸
        width = self.size().width()
        height = self.size().height()

        # 设置字体大小（按窗口宽度比例）
        new_size = int(width * 0.04)
        self.font.setPointSize(new_size)

        # 计算banner的合适大小（可以调整为你的需求）
        banner_width = width * 0.8  # 使用窗口宽度的80%
        banner_height = height * 0.2  # 使用窗口高度的20%

        # 计算居中位置
        x_pos = (width - banner_width) / 2
        y_pos = (height - banner_height) * 0.3

        # 设置banner几何位置和大小
        self.banner.setGeometry(x_pos, y_pos, banner_width, banner_height)

        # 设置字体
        self.banner.setFont(self.font)

        # 关键：设置文字对齐方式为居中
        self.banner.setAlignment(Qt.AlignCenter)

    def mouseDoubleClickEvent(self, event):
        """ mouseDoubleClickEvent(self, event: PySide2.QtGui.QMouseEvent) -> None """
        super().mouseDoubleClickEvent(event)
        if event.button() == Qt.LeftButton:  # 鼠标左键
            self.doubleClicked.emit()
